import { Parser, Builder } from 'xml2js';

let xml = '\
<?xml version="1.0" encoding="GB2312" ?>\
<Response>\
    <CmdType>Catalog</CmdType>\
    <SN>1</SN>\
    <DeviceID>41010200001320000001</DeviceID>\
    <DeviceID>41010200001320000001</DeviceID>\
    <SumNum>1</SumNum>\
    <DeviceList Num="1">\
        <Item>\
            <DeviceID>41010200001310000001</DeviceID>\
            <Name>IP PTZ Camera</Name>\
            <Manufacturer>Dahua</Manufacturer>\
            <Model>DH-SD-6A9230UA-HNI</Model>\
            <Owner>0</Owner>\
            <CivilCode>410102</CivilCode>\
            <Address>axy</Address>\
            <Parental>0</Parental>\
            <ParentID>41010200001320000001</ParentID>\
            <RegisterWay>1</RegisterWay>\
            <Secrecy>0</Secrecy>\
            <StreamNum>2</StreamNum>\
            <Status>ON</Status>\
            <Info>\
                <PTZType>3</PTZType>\
                <DownloadSpeed>1/2/4/8</DownloadSpeed>\
            </Info>\
        </Item>\
        <Item>\
            <DeviceID>41010200001310000001</DeviceID>\
            <Name>IP PTZ Camera</Name>\
            <Manufacturer>Dahua</Manufacturer>\
            <Model>DH-SD-6A9230UA-HNI</Model>\
            <Owner>0</Owner>\
            <CivilCode>410102</CivilCode>\
            <Address>axy</Address>\
            <Parental>0</Parental>\
            <ParentID>41010200001320000001</ParentID>\
            <RegisterWay>1</RegisterWay>\
            <Secrecy>0</Secrecy>\
            <StreamNum>2</StreamNum>\
            <Status>ON</Status>\
            <Info>\
                <PTZType>3</PTZType>\
                <DownloadSpeed>1/2/4/8</DownloadSpeed>\
            </Info>\
        </Item>\
    </DeviceList>\
</Response>\
';

async function test() {
    const parser = new Parser({ explicitArray: false });
    const json = await parser.parseStringPromise(xml);
    console.log(json);
    console.log(JSON.stringify(json,undefined,"\t"));
    const builder = new Builder();
    const content = builder.buildObject(json);
    console.log(content);
    throw new Error("什么鬼");
}

test().then(
    () => {
        console.log("Done");
    },
    (e) => {
        console.log("Failed");
        console.log(e);
    }
);